<?php
namespace Api\Abstracte;

/**
 *
 * @author zhongyong
 *        
 */
abstract class Object implements \Api\Interfaces\IBaseObject
{

    public $method = array();
 // 对象属性
    public $table;
 // 对象关联表名
    public function __construct()
    {
        $CI = & get_instance();
        $this->db = $CI->db;
    }

    /**
     *
     * @param unknown $parm            
     * @return \Api\Abstracte\Object
     */
    public function getObject($parm = array())
    {
        $result = $this->db->get_where($this->table, $parm)->row_array(0);
        foreach ($result as $key => $value) {
            $this->method[$key] = $value;
        }
        return $this;
    }

    /**
     *
     * @param unknown $parm            
     * @param string $where            
     */
    public function save($parm = array(), $where = null)
    {
        if (empty($where)) {
            $this->db->update($this->table, $parm, $where);
        } else {
            $this->db->insert($this->table, $parm);
        }
        return $this->db->affected_rows();
    }

    /**
     * 魔术方法，对象字符串
     */
    public function __toString()
    {
        return json_encode($this->method);
    }
}